package com.liveneo.adapter.mobile.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.service.LogService;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.request.OcrDriverLicenseRequest;
import com.liveneo.adapter.mobile.model.request.OcrImageRequest;
import com.liveneo.adapter.mobile.model.request.OcrPayeeInfoRequest;
import com.liveneo.adapter.mobile.service.OcrClaimService;
import com.liveneo.adapter.outinterface.util.DateUtil;
import com.liveneo.adapter.outinterface.util.ResultInfo;
import com.liveneo.adapter.outinterface.web.platform.ClaimInterface;
import com.liveneo.adapter.outinterface.web.platform.request.CliamUploadDto;
import com.liveneo.adapter.webservice.client.claim.OcrCar;
import com.liveneo.adapter.webservice.client.claim.OcrDrivingLicence;
import com.liveneo.adapter.webservice.client.claim.OcrDrivingPermit;
import com.liveneo.adapter.webservice.client.claim.OcrPay;
import com.liveneo.adapter.webservice.client.claim.OcrSurvey;
import com.liveneo.system.base.utils.Constant;
import com.liveneo.system.base.utils.OssManage;

@Service
public class OcrClaimServiceImpl implements OcrClaimService {
    @Autowired
    private LogService logService;

    /**
     * 调理赔接口p15 分行信息查询
     */
    @SuppressWarnings("deprecation")
    @Override
    public ResultInfo queryConstants(Long bank, Long city, String loguuid) throws Exception {
        ResultInfo resultInfo = null;
        try {
            resultInfo = ClaimInterface.synBranchInfo(bank, city);
            String reqMsg = "<分行信息查询>出参:reqMsg:logUuid=" + loguuid + ",bank=" + bank + ",city" + city;
            LogUtils.info(reqMsg);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.error("<分行信息查询>异常::logUuid=" + loguuid + ">" + e.toString());
            logService.addLogTodb(loguuid, "error", "", Constant.LOG_TYPE_CLAIM, "P15", "<分行信息查询>异常:" + e.toString());
        }
        return resultInfo;
    }

    @Override
    public ResultInfo uploadOCRinfo(OcrPayeeInfoRequest payee, OcrDriverLicenseRequest drivelicense, String loguuid, String type) throws Exception {
        ResultInfo resultInfo = null;
        OcrSurvey ocrSurvey = new OcrSurvey();
        if ("1".equals(type) && drivelicense != null) {   // 1:驾驶证信息；2：行驶证信息 3：身份证信息；4：银行信息
            ocrSurvey.setReportCode(drivelicense.getCaseNo());
            OcrCar ocrCar = new OcrCar();
            ocrCar.setTaskName(drivelicense.getObjectName());// 任务名称
            ocrCar.setTaskNo(drivelicense.getLossId());// 任务编号
            ocrCar.setTaskType(drivelicense.getLossType());// 任务类型
            // 驾驶证信息
            OcrDrivingLicence licence = new OcrDrivingLicence();
            licence.setCertCode(drivelicense.getDriverLicense());// 驾驶证号
            licence.setDriverLicenseno(drivelicense.getFileNo());// 驾驶员档案编号
            licence.setDriverPersonName(drivelicense.getDriver());// 驾驶人姓名
            licence.setPermitVehcode(drivelicense.getPermDriveCode());// 准驾车型
            ocrCar.setOcrDrivingLicence(licence);
            ocrSurvey.getOcrCars().add(ocrCar);
        } else if ("2".equals(type) && drivelicense != null) { // 1:驾驶证信息；2：行驶证信息 3：身份证信息；4：银行信息
            ocrSurvey.setReportCode(drivelicense.getCaseNo());
            OcrCar ocrCar = new OcrCar();
            ocrCar.setTaskName(drivelicense.getObjectName());// 任务名称
            ocrCar.setTaskNo(drivelicense.getLossId());// 任务编号
            ocrCar.setTaskType(drivelicense.getLossType());// 任务类型
            // 行驶证信息
            OcrDrivingPermit permit = new OcrDrivingPermit();
            permit.setEngineNo(drivelicense.getEngineCode());// 发动机号
            permit.setModelName(drivelicense.getBandModel());// 厂牌型号
            permit.setPlateNum(drivelicense.getLicensePlate());// 车牌号
            permit.setPlateType(drivelicense.getCarTypeCode());// 号牌种类
            permit.setVinNo(drivelicense.getCarIdentification());// Vin码
            permit.setRegdate(DateUtil.dateToXmlDate(DateUtil.parse(drivelicense.getRegistDate(), DateUtil.DEFAULT_PATTERN_DATE)));
            permit.setRissueDate(DateUtil.dateToXmlDate(DateUtil.parse(drivelicense.getIssueDate(), DateUtil.DEFAULT_PATTERN_DATE)));
            ocrCar.setOcrDrivingPermit(permit);
            ocrSurvey.getOcrCars().add(ocrCar);
        } else if ("3".equals(type) && payee != null) { // 1:驾驶证信息；2：行驶证信息 3：身份证信息；4：银行信息
            ocrSurvey.setReportCode(payee.getCaseNo());
            OcrCar ocrCar = new OcrCar();
            // 身份证信息
            OcrPay pay = new OcrPay();
            pay.setCertCode(payee.getPayeeCard());
            pay.setPersonName(payee.getPayeeName());
            pay.setInsuredPhone(payee.getInsuredPhone());
            ocrSurvey.setOcrPay(pay);
            ocrSurvey.getOcrCars().add(ocrCar);
        } else if ("4".equals(type) && payee != null) { // 1:驾驶证信息；2：行驶证信息 3：身份证信息；4：银行信息
            ocrSurvey.setReportCode(payee.getCaseNo());
            OcrCar ocrCar = new OcrCar();
            // 银行卡信息
            OcrPay pay = new OcrPay();
            pay.setCardNo(payee.getBankCard());
            pay.setBankName(payee.getBankCode());
            pay.setProvince(payee.getProvinceCode());
            pay.setCity(payee.getCityCode());
            pay.setSubsidiaryBank(payee.getBranchBankCode());
            pay.setSubbranch(payee.getBranchBank());
            if (StringUtils.endsWith("借记卡", payee.getCardType())) {
                pay.setCardType("1");
            } else if (StringUtils.endsWith("信用卡", payee.getCardType())) {
                pay.setCardType("2");
            }
            ocrSurvey.setOcrPay(pay);
            ocrSurvey.getOcrCars().add(ocrCar);
        }
        try {
            resultInfo = ClaimInterface.uploadOCRinfo2JY2G(ocrSurvey, type);
            String reqMsg = "<上传领款人信息和行驶证驾驶证信息>出参:reqMsg:logUuid=" + loguuid;
            LogUtils.info(reqMsg);
            // if (resultInfo.getResultCode() == "000" || "000".equals(resultInfo.getResultCode())) {
            // flag = true;
            // } else {
            // flag = false;
            // }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.error("<上传领款人信息和行驶证驾驶证信息>异常::logUuid=" + loguuid + ">" + e.toString());
            logService.addLogTodb(loguuid, "error", "", Constant.LOG_TYPE_CLAIM, "P16", "<上传领款人信息和行驶证驾驶证信息>异常:" + e.toString());
        }
        return resultInfo;
    }

    /**
     * 调用理赔接口MP04影像资料上传
     * */
    @Override
    public ResultInfo upoadImage(OcrImageRequest request, String loguuid) {
        ResultInfo resultInfo = null;
        try {
            OssManage ossMange = new OssManage();
            InputStream in = ossMange.getObject(request.getOSSKey());
            // InputStream in = new FileInputStream(new File("/Users/apple/Desktop/back.jpg"));
            List<CliamUploadDto> uploadDtoList = new ArrayList<CliamUploadDto>();
            CliamUploadDto uploadDto = new CliamUploadDto();
            uploadDto.setImgName(request.getImgName());
            uploadDto.setImgType(request.getImgType());
            uploadDto.setFileStream(in);
            uploadDtoList.add(uploadDto);
            resultInfo = ClaimInterface.upoadImage(request.getCaseNo(), uploadDtoList);
            String reqMsg = "<上传OCR图片信息>出参:reqMsg:logUuid=" + loguuid;
            LogUtils.info(reqMsg);
            // if (resultInfo.getResultCode() == "000" || "000".equals(resultInfo.getResultCode())) {
            // flag = true;
            // } else {
            // flag = false;
            // }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.error("<上传OCR图片信息>异常::logUuid=" + loguuid + ">" + e.toString());
            logService.addLogTodb(loguuid, "error", "", Constant.LOG_TYPE_CLAIM, "P04", "<上传OCR图片信息>异常:" + e.toString());
        }
        return resultInfo;
    }
}
